/*
 ============================================================================
 Name        : test_my_epoll.c
 Author      : liu.sh
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "Basic.h"
#include "Automated.h"
#include "Console.h"


////////////test case h file/////////////
#include "test_my_math.h"


CU_SuiteInfo *p_suite = NULL;

int add_suites(CU_SuiteInfo **pp_suite, CU_SuiteInfo *suite)
{
#define MOD_SUITE_NUM 10
	static CU_SuiteInfo *suite_array = NULL;
	static int suites_num = 0;

	if(suites_num % MOD_SUITE_NUM == 0)
	{
		suite_array = realloc(suite_array, 1 + suites_num + sizeof(CU_SuiteInfo) * MOD_SUITE_NUM);
	}

	suite_array[suites_num].pCleanupFunc = suite->pCleanupFunc;
	suite_array[suites_num].pInitFunc = suite->pInitFunc;
	suite_array[suites_num].pName = suite->pName;
	suite_array[suites_num].pTests = suite->pTests;

	++suites_num;

	suite_array[suites_num].pCleanupFunc = NULL;
	suite_array[suites_num].pInitFunc = NULL;
	suite_array[suites_num].pName = NULL;
	suite_array[suites_num].pTests = NULL;

	*pp_suite = suite_array;
	return suites_num;
}

void init_test_suites(CU_BasicRunMode mode)
{
	assert(NULL !=CU_get_registry());
	assert(!CU_is_test_running());

	add_suites(&p_suite, get_test_my_math_suite());

	if (CUE_SUCCESS != CU_register_suites(p_suite))
	{
		fprintf(stderr, "Register suites failed - %s ", CU_get_error_msg());
		exit(EXIT_FAILURE);
	}

	CU_basic_set_mode(mode);
}

int main(int argc,char * argv[])
{
	CU_BasicRunMode mode = CU_BRM_VERBOSE;
	CU_ErrorAction error_action = CUEA_IGNORE;

	setvbuf(stdout, NULL, _IONBF, 0);
	if (argc > 1)
	{
		if (!strcmp("-i", argv[1]))
		{
			CU_set_error_action(CUEA_IGNORE);
		}
		else if (!strcmp("-f", argv[1]))
		{
			CU_set_error_action(CUEA_FAIL);
		}
		else if (!strcmp("-A", argv[1]))
		{
			CU_set_error_action(CUEA_ABORT);
		}
		else if (!strcmp("-e", argv[1]))
		{
			//      print_example_results();00
		}
		else
		{
			printf(
					"\nUsage:  AutomatedTest [option]\n\n"
							"        Options: -i  Run, ignoring framework errors [default].\n"
							"                 -f  Run, failing on framework error.\n"
							"                 -A  Run, aborting on framework error.\n"
							"                 -e  Print expected test results and exit.\n"
							"                 -h  Print this message.\n\n");
		}
	}
	else
	{
		CU_set_error_action(error_action);
	}

	if (CU_initialize_registry())
	{
		printf("CU_initialize_registry error\n");
	}
	else
	{
		init_test_suites(mode);
		printf("\nTests completed with return value %d.\n", CU_basic_run_tests());

		CU_set_output_filename("DcmTest");
		CU_list_tests_to_file();
		CU_automated_run_tests();

		CU_cleanup_registry();
	}

	return 0;
}
